home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Files / Open_Selection.bsh < prev    next >
Text File  |  2013-07-28  |  5KB  |  160 lines

  1. /*
  2.  * Open_Selection.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - opens file named by selected text
  4.  * Copyright (C) 2001 Slava Pestov
  5.  * Copyright (C) 2012 Jarek Czekalski
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with the jEdit program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  *
  21.  * $Id: Open_Selection.bsh 22299 2012-10-04 11:43:29Z jarekczek $
  22.  *
  23.  * Checked for jEdit 4.0 API
  24.  *
  25.  */
  26.  
  27. String getBrowserPath() //{{{
  28. {
  29.     // copied from VFSBrowser constructor
  30.     // in 5.1 there will be VFSBrowser.getLastVisitedPath() api for that
  31.     HistoryModel pathModel = HistoryModel.getModel("vfs.browser.path");
  32.     if(pathModel.getSize() == 0)
  33.         return null;
  34.     else
  35.         return pathModel.getItem(0);
  36. } //}}}
  37.  
  38. // tryPath function {{{
  39. /** tryPath
  40.  * Tries to open a file built as concatenation of <code>sParent</code>
  41.  * and <code>sPath</code> paths. If file does not exist, no action is done.
  42.  * @return <code>true</code> if file exists, <code>false</code> otherwise.
  43.  */
  44. boolean tryPath(String sParent, String sPath)
  45. {
  46.     File f = new File(sParent, sPath);
  47.     if (f.exists())
  48.     {
  49.         jEdit.openFile(view, f.getPath());
  50.         return true;
  51.     }
  52.     else
  53.     {
  54.         return false;
  55.     }
  56. } //}}}
  57.  
  58. // getNoWordSep function {{{
  59. /**
  60.  * Inverts the list of word break chars to get the list of word
  61.  * separators. Ascii only space is assumed.
  62.  */
  63. String getNoWordSep(String sWordBreakChars)
  64. {
  65.     StringBuilder sb = new StringBuilder();
  66.     for (char c = 33; c <= 126; c++)
  67.     {
  68.         if (!Character.isLetterOrDigit(c)
  69.             && sWordBreakChars.indexOf(c) < 0)
  70.         {
  71.             sb.append(c);
  72.         }
  73.     }
  74.     return sb.toString();
  75. } //}}}
  76.  
  77. // getSelectedWords function {{{
  78. /**
  79.  * Returns an array of selected words, assuming that user selected
  80.  * words (not whitespaces). If nothing is selected, returns
  81.  * the word under the caret, using custom word break chars.
  82.  */
  83. String[] getSelectedWords()
  84. {
  85.     String sWordBreakChars = "\"\'";
  86.     ArrayList words = new ArrayList();
  87.     Selection[] sels = textArea.getSelection();
  88.     for (Selection sel: sels)
  89.         words.add(textArea.getSelectedText(sel));
  90.     if (words.size() == 0)
  91.     {
  92.         // we need to get the word under caret, as nothing is selected
  93.         // we use the same rules as in TextArea.selectWord
  94.         int nLine = textArea.getCaretLine();
  95.         CharSequence line = buffer.getLineSegment(nLine);
  96.         int nStartPos = textArea.getCaretPosition()
  97.                         - buffer.getLineStartOffset(nLine);
  98.  
  99.         // cannot read any char if at line end, so:
  100.         if (nStartPos == line.length())
  101.             nStartPos--;
  102.  
  103.         // findWordStart/End utilities expect noWordSep, but we have
  104.         /// a list of word separators - so invert it
  105.         String sNoWordSep = getNoWordSep(sWordBreakChars);
  106.  
  107.         int nLeft = TextUtilities.findWordStart(
  108.             line, nStartPos, sNoWordSep);
  109.         int nRight = TextUtilities.findWordEnd(
  110.             line, nStartPos+1, sNoWordSep);
  111.         if (nRight - nLeft < 1)
  112.         {
  113.             // javax.swing.JOptionPane.showMessageDialog(null,
  114.                 // "start: " + nLeft + ", end: " + nRight);
  115.             view.getToolkit().beep();
  116.         }
  117.         else
  118.             words.add(String.valueOf(line.subSequence(nLeft, nRight)));
  119.     }
  120.     return words.toArray(new String[0]);
  121. } //}}}
  122.  
  123. // openSelection function {{{
  124. /**
  125.  * Tries to find an existing file using current java directory and
  126.  * last browser directory as parents of the filename contained
  127.  * in selection. If this succeeds, the file is opened.
  128.  * Otherwise direct <code>jEdit.openFile</code> call is done on selection.
  129.  */
  130. void openSelection()
  131. {
  132.     for (String sPath: getSelectedWords())
  133.     {
  134.         if (!tryPath("", sPath) &&
  135.             !tryPath(getBrowserPath(), sPath))
  136.         {
  137.             jEdit.openFile(view, sPath);
  138.         }
  139.     }
  140. } //}}}
  141.  
  142. openSelection();
  143.  
  144. /* Macro index data (in DocBook format) {{{
  145.  
  146.             <listitem>
  147.                 <para><filename>Open_Selection.bsh</filename></para>
  148.  
  149.                 <para>Opens the file named by the current buffer's selected
  150.                 text. Current VFS browser directory is also tried as
  151.                 a parent of the filename, but only as a local path.</para>
  152.             </listitem>
  153.  
  154. }}} */
  155.  
  156.  
  157. // end Open_Selection.bsh
  158.  
  159. // :noTabs=false:tabSize=4:indentSize=4:folding=explicit:
  160.